package Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.support.v4.app.Fragment;

import java.util.List;

/**
 * @author BF00233
 */

public class ActivityUtil {

    public static boolean isActivityDestroyed(Activity activity) {
        return activity == null || activity.isFinishing();
    }

    public static boolean isFragmentDetached(Fragment fragment) {
        return fragment == null || fragment.isDetached();
    }

    public static boolean isFragmentDestroyed(Fragment fragment) {
        return fragment == null || fragment.isDetached() || fragment.isRemoving();
    }

    public static void finishThis(Activity activity) {
        if (!isActivityDestroyed(activity))
            activity.finish();
    }

    /**
     * 检验 Intent 是否有效
     */
    public static boolean validateIntent(Context context, Intent intent) {
        List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(intent, 0);
        return resolveInfoList != null && !resolveInfoList.isEmpty();
    }


    public static void checkNewTaskIntent(Context context, Intent intent) {
        if (context == null || intent == null) return;

        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
    }

}
